import { cloneDeep } from "lodash";
export default {
  methods: {
    // 获取logic数据
    getMenuList() {
      let filter = {
        className: this.className,
        type: this.type,
      };
      let newFilter = "";
      if (this.tools.isReal(filter)) {
        newFilter = this.classObject.util.getFilter(filter);
      }
      this.loading = true;
      this.classObject.query(newFilter).then((flag) => {
        if (flag) {
          this.loading = false;
        } else {
          this.loading = false;
        }
      });
    },
    handleInitMenuList() {
      this.menuList = cloneDeep(this.sourceMenuList);
      this.handleToggleExpandAll(true);
    },
    handleQueryNode(menuList, query, queryList) {
      menuList.forEach((menu) => {
        let newMenu = {};
        for (let i in menu) {
          if (i !== "children") newMenu[i] = cloneDeep(menu[i]);
        }
        if (newMenu.title.indexOf(query) > -1) queryList.push(newMenu);
        if (menu.children && menu.children.length) newMenu.children = [];
        menu.children && this.handleQueryNode(menu.children, query, queryList);
      });
      return queryList;
    },
    // 清空所有已选项
    handleClearSelect() {
      this.selectedData = [];
    },
    // 展开所有树
    handleToggleExpandAll(status) {
      this.toggleExpand = status;
      this.menuList = this.handleToggleExpandNode(this.menuList, status, []);
    },
    /**
     * @description 递归状态
     * @param {Array} menuList 原数据
     * @param {Boolean} isExpand 是否展开
     * @param {Array} lastList 递归数据
     * */
    handleToggleExpandNode(menuList, isExpand, lastList) {
      menuList.forEach((menu) => {
        let newMenu = {};
        for (let i in menu) {
          if (i !== "children") newMenu[i] = cloneDeep(menu[i]);
        }
        newMenu.expand = isExpand;
        newMenu.contextmenu = true;
        if (menu.children && menu.children.length) newMenu.children = [];
        lastList.push(newMenu);
        menu.children &&
          this.handleToggleExpandNode(
            menu.children,
            isExpand,
            newMenu.children
          );
      });
      return lastList;
    },
    // 添加hier中某项的接口
    hierAllAddRegister(pointer, newData, selectPanel) {
      this.loading = true;
      this.classObject
        .runAllAddRegister(pointer, { basic: newData })
        .then((flag) => {
          if (flag) {
            this.loading = false;
            this.$set(selectPanel.drawer, "show", false);
          } else {
            this.loading = false;
          }
        });
    },
    handleCheckNode(items) {
      this.selectedData = items;
    },
    // 打开新增弹框
    handleOpenCreateHeader() {
      this.$set(this.addNewPanel.drawer, "show", true);
      this.$emit("handleSelectNode", {
        id: "root",
        title: "root",
      });
    },
    handleOpenCreateSider() {
      this.$set(this.addNewPanel.drawer, "show", true);
    },
    handleSearch() {
      const query = this.query;
      this.selectedData = [];
      this.contextMenuInfo = null;
      if (query) {
        this.menuList = this.handleQueryNode(this.sourceMenuList, query, []);
      } else {
        this.handleInitMenuList();
      }
    },
    getOptionValue(options, value, key='label') {
      let dataValue = "";
      let index = options.findIndex((d) => d.value === value);
      if (index !== -1) {
        dataValue = options[index][key];
      }
      return dataValue;
    },
  },
};
